
import * as PIXI from 'pixi.js';
import Image from 'components/image';
import Text from 'components/text';
import beachBgTitle from 'common/asset/img/award/beachBgTitle.png';
import beachBgPeople from 'common/asset/img/award/beachBgPeople.png';
// import Text from 'components/text';
import CONST from 'common/const/award';
import DrawLottery from './drawLottery';
import ShareModal from './shareModal';
import Transition from './transition';
const { Container } = PIXI;

export default class Award {
  constructor({parent}) {

    this.CONST = CONST;
    this.parent = parent;
    this.container = new Container();
    this.hasAwarded = false;

    this.transition = new Transition({parent: this, onEnd: () => {
      setTimeout(() => {
        if (!this.bg) {
          this.container.removeChild(this.transition.container);
          this._renderBg();
        }
      }, 1000);
    }});

    this.container.addChild(this.transition.container);

    this.skipText = new Text({value: '跳 过', top: 50, left: 650, fontSize: 28, fill: 0xffffff});
    this.skipText.interactive = true;

    this.skipText.on('tap', () => {
      this._renderBg();
    });

    this.container.addChild(this.skipText);
  }

  _renderBg = () => {

    const { view } = window.CFG;

    this.bg = new Image({
      src: CONST.bg,
      width: view.width,
      height: view.height,
      left: 0,
      top: 0
    });
    this.container.addChild(this.bg);

    this.bgTitle = new Image({src: beachBgTitle, top: 200, width: 672});
    this.bgTitle.x = (view.width - this.bgTitle.width) / 2;
    this.container.addChild(this.bgTitle);


    this.bgPeople = new Image({src: beachBgPeople, width: 500, isAdapt: true});
    this.bgPeople.x = (view.width - this.bgPeople.width) / 2;
    this.bgPeople.y = view.height - this.bgPeople.height - 110;
    this.container.addChild(this.bgPeople);

    const drPlantToken = localStorage.getItem('sxPlantGameUserToken').split('-');

    // if (drPlantToken.length <= 1) {
    this._renderDrawLottery();
    // } else {
    //   // 抽过奖了
    //   this.hasAwarded = true;
    // }
  };

  _renderDrawLottery = () => {
    this.drawLottery = new DrawLottery({parent: this, CONST: this.CONST.drawLottery});
    this.container.addChild(this.drawLottery.container);
  };

  _renderShare = (hasCue) => {
    this.container.removeChild(this.drawLottery.container);
    const { share, playAgain, cue } = this.CONST;

    this.share = new ShareModal({parent: this, hasCue, CONST: {share, playAgain, cue}});
    this.container.addChild(this.share.container);
  };

};
